package cn.iocoder.yudao.module.design.controller.admin.relatived;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.CharUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.relatived.vo.*;
import cn.iocoder.yudao.module.design.convert.relatived.RelativedConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.relatived.RelativedDO;
import cn.iocoder.yudao.module.design.service.relatived.RelativedService;
import cn.iocoder.yudao.module.design.utils.ChineseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 关联人")
@RestController
@RequestMapping("/hd/relatived")
@Validated
public class RelativedController {

    @Resource
    private RelativedService relativedService;

    @PostMapping("/create")
    @Operation(summary ="创建关联人")
    @PreAuthorize("@ss.hasPermission('hd:relatived:create')")
    public CommonResult<Long> createRelatived(@Valid @RequestBody RelativedCreateReqVO createReqVO) {
        return success(relativedService.createRelatived(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新关联人")
    @PreAuthorize("@ss.hasPermission('hd:relatived:update')")
    public CommonResult<Boolean> updateRelatived(@Valid @RequestBody RelativedUpdateReqVO updateReqVO) {
        relativedService.updateRelatived(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除关联人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hd:relatived:delete')")
    public CommonResult<Boolean> deleteRelatived(@RequestParam("id") Long id) {
        relativedService.deleteRelatived(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得关联人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult<RelativedRespVO> getRelatived(@RequestParam("id") Long id) {
        RelativedDO relatived = relativedService.getRelatived(id);
        return success(RelativedConvert.INSTANCE.convert(relatived));
    }

    @GetMapping("/list")
    @Operation(summary ="获得关联人列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult<List<RelativedRespVO>> getRelativedList(@RequestParam("ids") Collection<Long> ids) {
        List<RelativedDO> list = relativedService.getRelativedList(ids);
        return success(RelativedConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得关联人分页")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult<PageResult<RelativedRespVO>> getRelativedPage(@Valid RelativedPageReqVO pageVO) {
        PageResult<RelativedDO> pageResult = relativedService.getRelativedPage(pageVO);
        return success(RelativedConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出关联人 Excel")
    @PreAuthorize("@ss.hasPermission('hd:relatived:export')")
   
    public void exportRelativedExcel(@Valid RelativedExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<RelativedDO> list = relativedService.getRelativedList(exportReqVO);
        // 导出 Excel
        List<RelativedExcelVO> datas = RelativedConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "关联人.xls", "数据", RelativedExcelVO.class, datas);
    }
    @PostMapping("/getRelativedRelList")
    @Operation(summary ="查询用户关联人")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult<List<Map>> getRelativedRelList( @RequestBody RelativedBaseVO vo) {
        List<Map> list = relativedService.getRelativedRelList(vo);
        return success(list);
    }
    @PostMapping("/getFriendList")
    @Operation(summary ="查询好友关联人")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult< List<Map<String, Object>>> getFriendList(  ) {
        List<Map> rawData = relativedService.getFriendList();
        // 转换后的数据
        List<Map<String, Object>> convertedData = new ArrayList<>();

        // 使用映射将首字母映射到对应的数据列表
        Map<Character, List<Map<String, String>>> mapping = new HashMap<>();

        for (Map<String, String> item : rawData) {
            String nickName = item.get("nickName");
            char firstChar = ChineseUtil.getFirstChar(nickName);
            if (CharUtil.isLetter(firstChar) || ChineseUtil.isChineseCharacter(firstChar)) {
                mapping.computeIfAbsent(firstChar, k -> CollUtil.newArrayList()).add(item);
            } else {
                mapping.computeIfAbsent('#', k -> CollUtil.newArrayList()).add(item);
            }
        }

        // 遍历映射中的分组，生成转换后的数据格式
        for (Map.Entry<Character, List<Map<String, String>>> entry : mapping.entrySet()) {
            Map<String, Object> convertedItem = MapUtil.newHashMap();
            convertedItem.put("letter", entry.getKey().toString().toUpperCase());
            convertedItem.put("data", entry.getValue());
            convertedData.add(convertedItem);
        }
        return success(convertedData);
    }

    @PostMapping("/getRelativedRelSupplierList")
    @Operation(summary ="查询用户关联供应商 ")
    @PreAuthorize("@ss.hasPermission('hd:relatived:query')")
    public CommonResult<List<RelativedSupplierVO>> getRelativedRelSupplierList( @RequestBody RelativedBaseVO vo) {

        List<RelativedSupplierVO> list = relativedService.getRelativedRelSupplierList(vo);
        return success(list);
    }
}
